/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.classloader.mutator;

import moved.org.objectweb.asm.ClassAdapter;
import moved.org.objectweb.asm.ClassReader;
import moved.org.objectweb.asm.ClassWriter;
import moved.org.objectweb.asm.MethodVisitor;
import picounit.features.classloader.NullVisitor;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

abstract public class DefaultClassMutator extends ClassAdapter implements ClassMutator {
	private final MethodMutator methodMutator;
	private final boolean computeMaxs;
	private final boolean skipDebug;

	private String className;

	public DefaultClassMutator(MethodMutator codeMutator) {
		this(codeMutator, true, false);
	}

	public DefaultClassMutator(MethodMutator codeMutator, boolean computeMaxs, boolean skipDebug) {
		super(NullVisitor.INSTANCE);

		this.methodMutator = codeMutator;
		this.computeMaxs = computeMaxs;
		this.skipDebug = skipDebug;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// ClassVisitor
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	@Override
	public void visit(int version, int access, String className, String signature, String superName,
		String[] interfaces) {

		this.className = className.replace('/', '.');

		super.visit(version, access, className, signature, superName, interfaces);
	}

	@Override
	public MethodVisitor visitMethod(int access, String methodName, String desc, String signature,
		String[] exceptions) {
		
		return methodMutator.mutate(new Method(access, className, methodName), 
			cv.visitMethod(access, methodName, desc, signature, exceptions));
	}
	
	public boolean shouldMutate(String className) {
		return true;
	}
	
	public final byte[] mutate(String className, byte[] byteCode) {
		ClassWriter classWriter = new ClassWriter(computeMaxs);
		
		cv = classWriter;

//		if ("example.collaborator.ParameterizedMockingTest".equals(className)) {
//			System.err.println("Original:");
//			traceClassVisitor.trace(byteCode);
//			System.err.println("Mutated:");
//			cv = new TraceClassDelegate(System.err, classWriter);
//		}

		new ClassReader(byteCode).accept(this, skipDebug);

		byte[] toByteArray = (classWriter).toByteArray();

		if (false && byteCode.length != toByteArray.length) {
			try {
				TraceClassVisitor traceClassVisitor = new TraceClassVisitor(System.err);
				System.out.println("Original Class: ");
				traceClassVisitor.trace(byteCode);
				System.out.println("Modified Class: ");
				traceClassVisitor.trace(toByteArray);

				String fileName = className.substring(className.lastIndexOf('.') + 1) + ".class";
				System.err.println("Writing different class: " +  fileName);

				FileOutputStream stream = new FileOutputStream("Modified" + fileName);
				stream.write(toByteArray);
				stream.close();
			}
			catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}

		return toByteArray;
	}
}
